import React from "react";
import { Button, Form, Input, message } from "antd";
import { getMenu } from "../../api";
import { useNavigate, Navigate } from "react-router-dom";
import "./login.css"

const Login = () => {
    const navigate = useNavigate()
    // 在登录状态下需要跳转到home页面
    if(localStorage.getItem('token')){
        return <Navigate to="/home" />
    }
    const handleSubmit = (val) => {
        if(!val.password || !val.account){
            return message.open({
                type: 'warning',
                content: '请输入用户名和密码'
            })
        }
        getMenu(val).then(({ data }) => {
            localStorage.setItem('token', data.data.token)
            navigate('/home')
        })
    }
    return (
        <Form className="login-container" onFinish={handleSubmit}>
            <div className="login_title">PT种子站登录</div>
            <Form.Item
                label="账号"
                name="account"
                rules={[
                    {
                        required: true,
                        message: '请输入账号',
                    },
                    {
                        pattern: /^[a-zA-Z0-9_-]{4,16}$/,
                        message: '账号由4-16位数字、字母、下划线、减号组成',
                    },
                ]}
            >
                <Input placeholder="请输入账号"/>
            </Form.Item>
            <Form.Item
                label="密码"
                name="password"
                rules={[
                    {
                        required: true,
                        message: '请输入密码',
                    },
                    {
                        pattern: /^[a-zA-Z0-9_-]{6,16}$/,
                        message: '密码由6-16位数字、字母、下划线、减号组成',
                    },
                ]}
            >
                <Input.Password placeholder="请输入密码" />
            </Form.Item>
            <Form.Item className="login-button">
                <Button type="primary" htmlType="submit">登录</Button>
            </Form.Item>
        </Form>
    )
}

export default Login

